<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="packageName">
        <el-input
          v-model="queryParams.packageName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="销售渠道" prop="distributionChannel">
        <el-select v-model="queryParams.distributionChannel" placeholder="请选择销售渠道" clearable>
          <el-option
            v-for="dict in dict.type.distribution_channel"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="商品类型" prop="kinds">
        <el-select v-model="queryParams.kinds" placeholder="请选择商品类型" clearable>
          <el-option
            v-for="dict in dict.type.kinds"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="套餐类型" prop="packageType">
        <el-select v-model="queryParams.packageType" placeholder="请选择套餐类型" clearable>
          <el-option
            v-for="dict in dict.type.package_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="销售渠道" prop="distributionChannel">
        <el-select v-model="queryParams.distributionChannel" placeholder="请选择销售渠道" clearable>
          <el-option
            v-for="dict in dict.type.svc_qd_name"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>


      <!--<el-form-item label="原价格" prop="originalPrice">
        <el-input
          v-model="queryParams.originalPrice"
          placeholder="请输入原价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="优惠后价格" prop="discountedPrice">
        <el-input
          v-model="queryParams.discountedPrice"
          placeholder="请输入优惠后价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库存" prop="inventory">
        <el-input
          v-model="queryParams.inventory"
          placeholder="请输入库存"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_goods_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="服务总次数" prop="serviceTotalNbr">
         <el-input
           v-model="queryParams.serviceTotalNbr"
           placeholder="请输入服务总次数"
           clearable
           @keyup.enter.native="handleQuery"
         />
       </el-form-item>-->
      <el-form-item label="上架时间">
        <el-date-picker
          v-model="daterangeListingTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="下架时间" prop="delistingTime">
        <el-date-picker clearable
                        v-model="queryParams.delistingTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择下架时间">
        </el-date-picker>
      </el-form-item>
      <!--<el-form-item label="创建时间 yyyy-MM-dd hh:mm:ss">
        <el-date-picker
          v-model="daterangeCreateDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>-->

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:packageinfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:packageinfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:packageinfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:packageinfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="packageinfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="唯一ID" align="center" prop="ckPackageInfoId" />



      <el-table-column label="商品名称" align="center" prop="packageName" />

      <el-table-column label="销售渠道" align="center" prop="distributionChannel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.svc_qd_name" :value="scope.row.distributionChannel"/>
        </template>
      </el-table-column>

      <el-table-column label="商品类型" align="center" prop="kinds">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.kinds" :value="scope.row.kinds"/>
        </template>
      </el-table-column>
     <!-- <el-table-column label="原价格" align="center" prop="originalPrice" />
      <el-table-column label="优惠价" align="center" prop="discountedPrice" />
      <el-table-column label="库存" align="center" prop="inventory" />-->
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_goods_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="首图" align="center" prop="thumbnail" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.thumbnail" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="轮播图" align="center" prop="carouselChart" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.carouselChart" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="详情图" align="center" prop="detailedDiagram" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.detailedDiagram" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="套餐类型" align="center" prop="packageType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.package_type" :value="scope.row.packageType"/>
        </template>
      </el-table-column>
      <el-table-column label="服务阶段" align="center" prop="servicePhase">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.service_phase" :value="scope.row.servicePhase ? scope.row.servicePhase.split(',') : []"/>
        </template>
      </el-table-column>

      <el-table-column label="服务总次数" align="center" prop="serviceTotalNbr" />
      <el-table-column label="上架时间" align="center" prop="listingTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.listingTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下架时间" align="center" prop="delistingTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.delistingTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="notes" />
      <el-table-column label="创建时间" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate) }}</span>
        </template>
      </el-table-column>



      <el-table-column label="操作" align="center"  width="120" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:packageinfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:packageinfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改套餐对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">


        <el-form-item label="商品名称" prop="packageName">
          <el-input v-model="form.packageName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="销售渠道" prop="distributionChannel">
          <el-select v-model="form.distributionChannel" placeholder="请选择销售渠道">
            <el-option
              v-for="dict in dict.type.svc_qd_name"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品类型" prop="kinds">
          <el-select v-model="form.kinds" placeholder="请选择商品类型">
            <el-option
              v-for="dict in dict.type.kinds"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
       <!-- <el-form-item label="原价格" prop="originalPrice">
          <el-input v-model="form.originalPrice" placeholder="请输入原价格" />
        </el-form-item>
        <el-form-item label="优惠价" prop="discountedPrice">
          <el-input v-model="form.discountedPrice" placeholder="请输入优惠价" />
        </el-form-item>
        <el-form-item label="库存" prop="inventory">
          <el-input v-model="form.inventory" placeholder="请输入库存" />
        </el-form-item>-->
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_goods_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="首图">
          <image-upload v-model="form.thumbnail"/>
        </el-form-item>
        <el-form-item label="轮播图">
          <image-upload v-model="form.carouselChart"/>
        </el-form-item>
        <el-form-item label="详情图">
          <image-upload v-model="form.detailedDiagram"/>
        </el-form-item>

        <el-form-item label="套餐类型" prop="packageType">
          <el-select v-model="form.packageType" placeholder="请选择套餐类型 1-单一套餐 2-组合套餐">
            <el-option
              v-for="dict in dict.type.package_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="服务阶段">
          <el-checkbox-group v-model="form.servicePhase">
            <el-checkbox
              v-for="dict in dict.type.service_phase"
              :key="dict.value"
              :label="dict.value">
              {{dict.label}}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="服务总次数" prop="serviceTotalNbr">
          <el-input v-model="form.serviceTotalNbr" placeholder="请输入服务总次数" />
        </el-form-item>
        <el-form-item label="上架时间" prop="listingTime">
          <el-date-picker clearable
                          v-model="form.listingTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择上架时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="下架时间" prop="delistingTime">
          <el-date-picker clearable
                          v-model="form.delistingTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择下架时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="notes">
          <el-input v-model="form.notes" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <!--<el-form-item label="创建时间" prop="createDate">
          <el-date-picker clearable
                          v-model="form.createDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择创建时间 ">
          </el-date-picker>
        </el-form-item>-->

        <el-divider content-position="center">商品规格信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddCkProductSpecification">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteCkProductSpecification">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="ckProductSpecificationList" :row-class-name="rowCkProductSpecificationIndex" @selection-change="handleCkProductSpecificationSelectionChange" ref="ckProductSpecification">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <!--<el-table-column label="商品名称" prop="packagename" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.packagename" placeholder="请输入商品名称" />
            </template>
          </el-table-column>-->
          <el-table-column label="规格信息" prop="specification" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.specification" placeholder="请输入规格信息" />
            </template>
          </el-table-column>
          <el-table-column label="原价" prop="originalPrice" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.originalPrice" placeholder="请输入原价" />
            </template>
          </el-table-column>
          <el-table-column label="优惠价" prop="discountedPrice" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.discountedPrice" placeholder="请输入优惠价" />
            </template>
          </el-table-column>
          <!--<el-table-column label="备注" prop="remark" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入备注" />
            </template>
          </el-table-column>-->
          <el-table-column label="库存" prop="inventory" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.inventory" placeholder="请输入库存" />
            </template>
          </el-table-column>
        </el-table>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listPackageinfo, getPackageinfo, delPackageinfo, addPackageinfo, updatePackageinfo } from "@/api/system/packageinfo";

  export default {
    name: "Packageinfo",
    dicts: ['service_phase', 'sys_goods_status','svc_qd_name', 'kinds', 'package_type', 'distribution_channel'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 子表选中数据
        checkedCkProductSpecification: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 商品规格表格数据
        ckProductSpecificationList: [],
        // 总条数
        total: 0,
        // 套餐表格数据
        packageinfoList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 销售渠道时间范围
        daterangeListingTime: [],
        // 销售渠道时间范围
        daterangeCreateDate: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          packageName: null,
          packageType: null,
          originalPrice: null,
          discountedPrice: null,
          inventory: null,
          status: null,
          thumbnail: null,
          carouselChart: null,
          detailedDiagram: null,
          serviceTotalNbr: null,
          listingTime: null,
          delistingTime: null,
          notes: null,
          createDate: null,
          servicePhase: null,
          kinds: null,
          distributionChannel: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          packageName: [
            { required: true, message: "套餐名称不能为空", trigger: "blur" }
          ],

          /* originalPrice: [
             { required: true, message: "原价格不能为空", trigger: "blur" }
           ],
           discountedPrice: [
             { required: true, message: "优惠价不能为空", trigger: "blur" }
           ],
           inventory: [
             { required: true, message: "库存不能为空", trigger: "blur" }
           ],*/
          status: [
            { required: true, message: "状态不能为空", trigger: "blur" }
          ],
          thumbnail: [
            { required: true, message: "首图不能为空", trigger: "blur" }
          ],
          carouselChart: [
            { required: true, message: "轮播图不能为空", trigger: "blur" }
          ],
          detailedDiagram: [
            { required: true, message: "详情图不能为空", trigger: "blur" }
          ],

          listingTime: [
            { required: true, message: "上架时间不能为空", trigger: "blur" }
          ],

          servicePhase: [
            { required: true, message: "服务阶段不能为空", trigger: "blur" }
          ],

          kinds: [
            { required: true, message: "商品类型不能为空", trigger: "change" }
          ],
          distributionChannel: [
            { required: true, message: "销售渠道不能为空", trigger: "change" }
          ]
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询套餐列表 */
      getList() {
        this.loading = true;
        this.queryParams.params = {};
        if (null != this.daterangeListingTime && '' != this.daterangeListingTime) {
          this.queryParams.params["beginListingTime"] = this.daterangeListingTime[0];
          this.queryParams.params["endListingTime"] = this.daterangeListingTime[1];
        }
        if (null != this.daterangeCreateDate && '' != this.daterangeCreateDate) {
          this.queryParams.params["beginCreateDate"] = this.daterangeCreateDate[0];
          this.queryParams.params["endCreateDate"] = this.daterangeCreateDate[1];
        }
        listPackageinfo(this.queryParams).then(response => {
          this.packageinfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          ckPackageInfoId: null,
          packageName: null,
          packageType: null,
          originalPrice: null,
          discountedPrice: null,
          inventory: null,
          status: "0",
          thumbnail: null,
          carouselChart: null,
          detailedDiagram: null,
          serviceTotalNbr: null,
          listingTime: null,
          delistingTime: null,
          notes: null,
          createDate: null,
          servicePhase: [],
          kinds: null,
          distributionChannel: null
        };
        this.ckProductSpecificationList = [];
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.daterangeListingTime = [];
        this.daterangeCreateDate = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.ckPackageInfoId)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加商品";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const ckPackageInfoId = row.ckPackageInfoId || this.ids
        getPackageinfo(ckPackageInfoId).then(response => {
          this.form = response.data;
          this.form.servicePhase = this.form.servicePhase.split(",");
          this.ckProductSpecificationList = response.data.ckProductSpecificationList;
          this.open = true;

          this.title = "修改商品";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.form.servicePhase = this.form.servicePhase.join(",");
            this.form.ckProductSpecificationList = this.ckProductSpecificationList;
            if (this.form.ckPackageInfoId != null) {
              updatePackageinfo(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addPackageinfo(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ckPackageInfoIds = row.ckPackageInfoId || this.ids;
        this.$modal.confirm('是否确认删除商品编号为"' + ckPackageInfoIds + '"的数据项？').then(function() {
          return delPackageinfo(ckPackageInfoIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 商品规格序号 */
      rowCkProductSpecificationIndex({ row, rowIndex }) {
        row.index = rowIndex + 1;
      },
      /** 商品规格添加按钮操作 */
      handleAddCkProductSpecification() {
        let obj = {};
       /* obj.packagename = "";*/
        obj.specification = "";
        obj.originalPrice = "";
        obj.discountedPrice = "";
       /* obj.remark = "";*/
        obj.inventory = "";
        this.ckProductSpecificationList.push(obj);
      },
      /** 商品规格删除按钮操作 */
      handleDeleteCkProductSpecification() {
        if (this.checkedCkProductSpecification.length == 0) {
          this.$modal.msgError("请先选择要删除的商品规格数据");
        } else {
          const ckProductSpecificationList = this.ckProductSpecificationList;
          const checkedCkProductSpecification = this.checkedCkProductSpecification;
          this.ckProductSpecificationList = ckProductSpecificationList.filter(function(item) {
            return checkedCkProductSpecification.indexOf(item.index) == -1
          });
        }
      },
      /** 复选框选中数据 */
      handleCkProductSpecificationSelectionChange(selection) {
        this.checkedCkProductSpecification = selection.map(item => item.index)
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/packageinfo/export', {
          ...this.queryParams
        }, `packageinfo_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
